var t = getApp()
Page({
  data: {
    change:0,
    page:1,
    total:0,
    canpre:false,
    cannext:false,
    log:[],
    wash_log:[],
    navHeight:t.globalData.navHeight,
    title:'消费记录',
  },
  onLoad: function (options) {
    this.setData({
      change:options.index
    })
  },
  onShow(e){
    this.getList()
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.getList()
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  getList2(e){
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    t.request('/users/wallet_log',{},function(res){
      if (res.code==200) {
        var wash = res.result.wash_log
        for (let i = 0; i < wash.length; i++) {
          wash[i].in_time = that.getDate(wash[i].in_time*1000)
          wash[i].out_time = that.getDate(wash[i].out_time*1000).split(' ')[1]
          console.log(wash[i].out_time);
        }
        that.setData({
          log:res.result.log,
          wash_log:res.result.wash_log
        })
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  getList(e){
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    let change=that.data.change;
    if(change==1){
      t.request('/users/Washing_log',{page:this.data.page},function(res){
        if (res.code==200) {
         console.log(res);
         let cannext=false;
         if(res.result.total>that.data.page){
          cannext=true;
        }
         that.setData({
          wash_log:res.result.data,
          total:res.result.total,
          cannext:cannext
        })
        wx.hideLoading({
          success: (res) => {},
        })
        }
      })
    }else{
      t.request('/users/Recharge_log',{page:this.data.page},function(res){
        if (res.code==200) {
         console.log(res);
         let cannext=false;
         if(res.result.total>that.data.page){
          cannext=true;
        }
         that.setData({
          log:res.result.data,
          total:res.result.total,
          cannext:cannext
        })
        wx.hideLoading({
          success: (res) => {},
        })
        }
      })
    }
    
  },
  getDate(temp){
    var time = new Date(temp)
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    console.log(m);
    m = m.toString().length>1?m:'0'+m
    d = d.toString().length>1?d:'0'+d
    h = h.toString().length>1?h:'0'+h
    mm = mm.toString().length>1?mm:'0'+mm
    s = s.toString().length>1?s:'0'+s
    return y+'-'+m+'-'+d+' '+h+':'+mm+':'+s;
  },
  change(e){
    this.setData({
      change:e.currentTarget.dataset.index,
      page:1
    })
    this.getList();
  },
  gopre(){
    this.setData({
      page:this.data.page-1
    })
    this.getList();
  },
  gonext(){
    this.setData({
      page:this.data.page+1
    })
    this.getList();
  }
})